################################################################################
### Technological Change and the International System Replication Files
### Figure 1. Trends in (Delta Log of) Technology Units per Capita of twenty key technologies from
### Comin et al. (2013) from 1820 to 2009
###  
### Required data files: "full_data.dta"
###                    
### Created by: Thomas Cunningham
### Date: 27 January 2021
##################################################################

# Start from Clean Work Space
rm(list = ls())

# Set Working Directory
setwd(dirname(rstudioapi::getActiveDocumentContext()$path)) 
# Note: if you are not using R Studio this command will not work, set WD to source file location manually

require(readstata13)
require(ggplot2)
require(dplyr)

dta <- read.dta13("full_data.dta")

# Subsetting to solely those observations in which the variable of interest for 
# Figure 1 exist

# Description: As described in the Figure caption, 
# delta_c_and_t_plus_y is calculated by taking the residuals of the
# regression where the DV is change in log adoption level per capita (delta)
# for the uncensored data and the independent variables are country and technology fixed effects 
# This is then added to the overall mean of delta
data <- dta %>%
  filter(!(is.na(delta_c_and_t_plus_y))) %>%
  select(year, delta_c_and_t_plus_y)

p <- ggplot(data[data$year %in% 1820:2009, ], 
            aes(y=delta_c_and_t_plus_y*100, 
                x=year))+geom_smooth(span = 0.01, 
                                     col = "black")+theme_bw()+ylab("")+xlab("")+coord_cartesian(xlim = NULL, 
                                                                                                 ylim = c(0.00, 7), 
                                                                                                 expand = TRUE)+scale_y_continuous(labels = function(x) paste0(x, "%"))


print(p)

pdf(file="output/figure1.pdf",width=6,height=5)
print(p)
dev.off()

